<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Session;
class CartController extends Controller
{
    //
    public function add($id)
    {
    	if(session('user_info'))
    	{
            $iscart =DB::table('cart') -> where([['gid','=',$id],['uid','=',Session('user_info') -> id]]) -> first();
            if($iscart)
            {  
                $data['bcnt'] = $iscart -> bcnt + 1 ;
                $res = DB::table('cart') -> where([['gid','=',$id],['uid','=',Session('user_info') -> id]]) -> update($data);
            }
            else{
                $data = DB::table('goods') -> where('gid',$id) -> first();
                $cart['uid'] = Session('user_info') -> id;
                $cart['uname'] = Session('user_info') -> name;
                $cart['gid'] = $data -> gid;
                $cart['bcnt'] = 1;
                $res = DB::table('cart') ->insert([$cart]);
            }
	    	



    		if($res)
    		{
    			return back() -> with(['info' => '添加购物车成功']);
    		}else
    		{
    			return back() -> with(['info' => '添加购物车失败']);
    		}

    	}else
    	{
    		return view('home.user.login',['title'=>'用户登录']);
    	}
    }

    public function index()
    {
    	if(Session('user_info'))
    	{
    		$data = DB::table('cart') -> where('uid',Session('user_info')->id) -> get();

    		$num = 0;
    		foreach($data as $row)
    		{
    			$res = DB::table('goods') -> where('gid',$row -> gid) -> first();
    			$row -> gid = $res;
    			$num += ($res -> gpirce)*($row -> bcnt);
    		}
    		return view('home.cart.index',['title'=>'购物车','data'=>$data,'num' => $num]);

    	}else
    	{
    		return view('home.user.login',['title'=>'用户登录']);
    	}
    }

    public function ajaxjia(Request $request)
    {
    	
    	$gname = $request -> gname;
    

    	$gid = DB::table('goods') -> where('gname',$gname) -> first() -> gid;
    	$gpirce = DB::table('goods') -> where('gid',$gid) -> first() -> gpirce;
    	

    	$uid = Session('user_info') -> id;
    	$pcnt = $request -> pcnt + 1;
    	$pcnts = $gpirce * $pcnt;
    	// $list['jiacha'] = ($pcnt - $old)*$gpirce;

    	$list['pcnts'] = $pcnts;
    	$list['pcnt'] = $pcnt;
    	$res = DB::table('cart') -> where([['gid','=',$gid],['uid','=',$uid]]) -> update(['bcnt'=>$pcnt]);
    	
    	if($res)
    	{

    		$data = DB::table('cart') -> where('uid',Session('user_info')->id) -> get();
    		
    		$list['num'] = 0;
    		foreach($data as $row)
    		{
    			$res = DB::table('goods') -> where('gid',$row -> gid) -> first();
    			$row -> gid = $res;
    			$list['num'] += ($res -> gpirce)*($row -> bcnt);
    		}
    		return $list;

    	}else
    	{
    		return 1;
    	}
    	
    }

     public function ajaxjian(Request $request)
    {
    	// return '111';
    	$gname = $request -> gname;
    

    	$gid = DB::table('goods') -> where('gname',$gname) -> first() -> gid;
    	$gpirce = DB::table('goods') -> where('gid',$gid) -> first() -> gpirce;
    	

    	$uid = Session('user_info') -> id;
    	$pcnt = $request -> pcnt - 1;
    	// return $pcnt;
    	$pcnts = $gpirce * $pcnt;
    	// $list['jiacha'] = ($pcnt - $old)*$gpirce;

    	$list['pcnts'] = $pcnts;
    	$list['pcnt'] = $pcnt;
    	$res = DB::table('cart') -> where([['gid','=',$gid],['uid','=',$uid]]) -> update(['bcnt'=>$pcnt]);
    	
    	if($res)
    	{

    		$data = DB::table('cart') -> where('uid',Session('user_info')->id) -> get();
    		
    		$list['num'] = 0;
    		foreach($data as $row)
    		{
    			$res = DB::table('goods') -> where('gid',$row -> gid) -> first();
    			$row -> gid = $res;
    			$list['num'] += ($res -> gpirce)*($row -> bcnt);
    		}
    		return $list;

    	}else
    	{
    		return 1;
    	}
    	
    }

    public function del($id)
    {
        $res =  DB::table('cart') -> where('id',$id) ->delete();
        if($res)
        {
            return back() -> with(['info' => '移出购物车成功']);
        }else
        {
            return back() -> with(['info' => '移出购物车失败']);
        }
        
    }

    public function delete()
    {
        $id = Session('user_info') -> id;
        $res = DB::table('cart') -> where('uid',$id) -> delete();
        if($res)
        {
            return back() -> with(['info' => '购物车已经没有商品了']);
        }
        
    }




}
